﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;

namespace Project_Udrinkwater.CustomerLogin
{
    public partial class ShowOrderList : System.Web.UI.Page
    {
        DataClassAddOrderDataContext db = new DataClassAddOrderDataContext();
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           BindGrid();
        }
        protected void BindGrid()
        {
            GV_OrderList.DataSource = Session["TablePrice"] as DataTable;
            GV_OrderList.DataBind();
        }

        protected void GV_OrderList_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            //if (e.Column.FieldName == "Number")
            //{
            //    e.Value = string.Format("{0}", e.ListSourceRowIndex + 1);
            //}
            if (e.Column.FieldName == "TotalRow")
            {
                int price = (int)e.GetListSourceFieldValue("Price");
                int PriceNumber = (int)e.GetListSourceFieldValue("PriceNumber");
                e.Value = price * PriceNumber;
            }

        }

        protected void bt_InsertData_Click(object sender, EventArgs e)
        {
            if (Session["TablePrice"] != null)
            {
                DataTable dt = new DataTable();
                dt = (DataTable)Session["TablePrice"];

               // Data DataInsert = new Data();
               // DataInsert.Empoyee_ID = "INTERNET";
               // DataInsert.Customer = "Customer1";//เปลี่ยนตามชื่อ Login
               // DataInsert.OrderDate = DateTime.Now;
               // DataInsert.DeadLine = DateTime.Now.AddDays(3);
               //// DataInsert.Discount = 0;

               // InsertTime Insert = new InsertTime();
               // Insert.Inserts_Order(DataInsert);
                TB_Order od = new TB_Order();
                od.Employee_ID = "INTERNET";
                od.Customer_ID = Session["User"].ToString();
                od.Order_Date_Time = DateTime.Now;
                od.Deadlines_Date = DateTime.Now.AddDays(3);
                db.TB_Orders.InsertOnSubmit(od);
                db.SubmitChanges();

                string T1 = DateTime.Now.ToString("yyyy-MM-dd", new System.Globalization.CultureInfo("en-US"));
                DataTable Addlist = new DataTable();
                Addlist = GetData_OrderIDlist(T1);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TB_Order_List odl = new TB_Order_List();
                    odl.Order_ID = Convert.ToInt32(Addlist.Rows[0]["Order_ID"].ToString());
                    DataTable IDcate = new DataTable();
                    IDcate = GetData_Category(dt.Rows[i]["Goods_ID"].ToString());
                    odl.Order_Category_ID = Convert.ToInt32(IDcate.Rows[0]["Order_Category_ID"].ToString());
                    odl.Product_ID = dt.Rows[i]["Goods_ID"].ToString();
                    odl.Amount = Convert.ToInt32(dt.Rows[i]["PriceNumber"].ToString());
                    odl.Price_Unit = Convert.ToInt32(dt.Rows[i]["Price"].ToString());
                    odl.Discount = 0;
                    db.TB_Order_Lists.InsertOnSubmit(odl);
                    db.SubmitChanges();
                }
                Response.Write(@"<script language='javascript'>alert('บันทึกข้อมูลการซื้อเรียบร้อยแล้ว')</script>");
                Session["TablePrice"] = null;
            }
        }
        private DataTable GetData_OrderID(string GoodID)
        {
            string sqltext = @"SELECT * FROM [TB_ImagePath] WHERE [Image_ID] = '" + GoodID + "' ";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }
        public DataTable GetData_OrderIDlist(string T1)
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT * FROM [TB_Order] WHERE [Customer_ID] = '" + Session["User"].ToString() + "'" +
                                 "AND (Order_Date_Time BETWEEN '" + T1 + " 00:00:00' AND '" + T1 + " 23:59:59')";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetData_Category(string Goods)
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT * FROM [TB_Order_Category] WHERE [Order_Category_Name] = 'ขายปลีก'";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }

        protected void GV_OrderList_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Delete")
            {
               // Response.Redirect("PromotionLogin.aspx");
                string Index = e.CommandArgs.CommandArgument.ToString();
                DataTable dt = (DataTable)Session["TablePrice"];
                dt.Rows[Convert.ToInt32(Index)].Delete();
                Session["TablePrice"] = dt;
                BindGrid();
            }
        }

     
        }
       
       
    }
